<template>
	<view>
		<uni-card title="使用说明">
			<view>1、请打开手机蓝牙功能；</view>
			<view>2、请打开设备开关，保持开机状态；</view>
		</uni-card>
		<view v-if="list.length>0" v-for="(item,index) in list" :key="index">
			<uni-card v-if="item.name.indexOf('SCZN')>-1" @click="add(item)">
				<view style="display: flex;height: 55rpx;line-height: 55rpx">
					<view><text style="font-size: 30rpx;">设备号:{{ item.name }}</text></view>
					<view style="flex: 1;text-align: right;"><button size="mini" type="primary">绑定</button></view>
				</view>
			</uni-card>
		</view>
		<view v-else style="text-align: center;">
			<uni-card>
				<text>没有可用蓝牙设备，请检查蓝牙是否开启?</text>
			</uni-card>
		</view>

	</view>
</template>

<script>
	import {
		Blue
	} from '@/utils/index.js'
	import {
		ConnectController
	} from '@/utils/controller.js'
	const mini_cz_obj = uniCloud.importObject('wmz-cz-mini')
	export default {
		data() {
			return {
				state: '',
				list: [],
				connectDeviceId: ''
			}
		},
		onLoad() {
			Blue.start()
			ConnectController.addConnectStateListen(data => {
				this.state = data.label
				if (data.deviceId) {
					this.connectDeviceId = data.deviceId
				}
			})
			ConnectController.addDevicesListen(device => {
				const index = this.list.findIndex(item => item.name === device.name)
				if (index < 0) {
					this.list.push(device)
				}
			})
		},
		onUnload() {
			Blue.closeBlue()
		},
		methods: {
			async add(item) {
				const addBluetoothRes = await mini_cz_obj.addBluetooth({
					device_id: item.deviceId,
					name: item.name,
					open_id: uni.getStorageSync('wmz-nexmove-cz-open-id')
				})
				if (addBluetoothRes.code === 200) {
					if (addBluetoothRes.data === 2) {
						uni.navigateBack()
					} else {
						uni.showToast({
							title: '配对成功!',
							icon: 'success'
						})
					}
				} else {
					{
						uni.showToast({
							title: addBluetoothRes.msg,
							icon: 'error'
						})
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>